//
//  ADViewController.m
//  gosh
//
//  Created by 许小军 on 16/9/22.
//  Copyright © 2016年 wyzc. All rights reserved.
//

#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

#import "ADViewController.h"
#import "XXJTabBarController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <UIImageView+WebCache.h>
#import "XXJADModel.h"

@interface ADViewController ()


@property (weak, nonatomic) IBOutlet UIView *adView;
@property (weak, nonatomic) IBOutlet UIButton *jumpClick;
@property(strong,nonatomic)UIImageView * adImageView;
@property(strong,nonatomic)XXJADModel * model;
@property(strong,nonatomic)NSTimer * timer;
@end

@implementation ADViewController
-(UIImageView *)adImageView
{
    if (_adImageView==nil) {
        
        _adImageView=[[UIImageView alloc]init];
        [self.adView addSubview:_adImageView];
        _adImageView.userInteractionEnabled=YES;
        
        UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [_adImageView addGestureRecognizer:tap];
        
    }
    return _adImageView;
}

-(void)tap
{
    NSURL * url=[NSURL URLWithString:_model.ori_curl];
    UIApplication * app=[UIApplication sharedApplication];
    if ([app canOpenURL:url]) {
        [app openURL:url];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self loadAdData];
    
    _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    
}
- (IBAction)jumpClick:(UIButton *)sender {
    [self animationView];
    XXJTabBarController * tab=[[XXJTabBarController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController=tab;
    [_timer invalidate];
}

-(void)timeChange
{
    static int i=3;
    if (i==0) {
        [self jumpClick:nil];
    }
    i--;
    [_jumpClick setTitle:[NSString stringWithFormat:@"跳转(%ds)",i] forState:UIControlStateNormal];
}

-(void)loadAdData
{
    AFHTTPSessionManager * magr=[AFHTTPSessionManager manager];
    NSMutableDictionary * parameters=[NSMutableDictionary dictionary];
    parameters[@"code2"]=code2;
    
    [magr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary * adDict=[responseObject[@"ad"] lastObject];
        _model=[XXJADModel mj_objectWithKeyValues:adDict];
        //CGFloat h=self.view.cur_w/_model.w*_model.h;
        
        self.adImageView.frame=CGRectMake(0, 0, self.view.cur_w, self.view.cur_h);
        [self.adImageView sd_setImageWithURL:[NSURL URLWithString:_model.w_picurl]];
        
                               
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
    
}

-(void)animationView
{
    [UIView animateWithDuration:0.8 animations:^{
        [self.adImageView setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.2)];
        [self.adImageView setAlpha:0];
        //self.adView.alpha=0;
    } completion:^(BOOL finished) {
        //[self.adImageView removeFromSuperview];
        //[self.adView removeFromSuperview];
        //self.adView=nil;
        XXJTabBarController * tab=[[XXJTabBarController alloc]init];
        [UIApplication sharedApplication].keyWindow.rootViewController=tab;
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
